﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace FileChat
{
    public partial class FormLogin : Form
    {

        public MainForm MyMainForm { get; set; }
        public FormLogin()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            RegForm frm = new RegForm();
            frm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.Text == "")
                        {
                            MessageBox.Show("为了便于沟通，所有内容都需要填写哟！");
                            return;
                        }
                    }
                }

                string fileChatPath = string.Format("{0}\\FileChatData.xml", txtFileChatPath.Text);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileChatPath);
                XmlNode root = xmlDoc.SelectSingleNode("FileChat/UserList");
                User user = GetUser(root,txtUsername.Text.Trim(),txtPassword.Text.Trim());
                if (user == null)
                {
                    MessageBox.Show("验证不通过！请检查用户与密码！");
                    return;
                }
                else {
                    //MessageBox.Show("登陆成功！");
                    //设置状态
                    foreach (XmlElement usernode in root.ChildNodes)
                    {
                        if (usernode.Attributes["Username"].Value == txtUsername.Text.Trim())
                        {
                            usernode.Attributes["Status"].Value = "online";
                        }
                    }


                    MainForm frm = MyMainForm;
                    frm.FileChatPath = fileChatPath;
                    frm.CurrentUser = user;
                    frm.FileChatBasePath = txtFileChatPath.Text;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                   
                }

              

            }
            catch (Exception ex)
            {
                MessageBox.Show("出错啦，请确认共享路径设置正确！");
                Console.Write(ex.Message);
            }
          
        }

        private void SetUserStatus(User user)
        {
            try
            {
                string fileChatPath = string.Format("{0}\\FileChatData.xml", txtFileChatPath.Text);
                //向目标用户的文件夹里写一个xml聊天信息 或二进制聊天信息流
                string targetFilePath = string.Format(String.Format("{0}/GlobalData/{1}/status", fileChatPath, user.ID));
                string msgPath = string.Format("{0}/{1}.xml", targetFilePath, string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now));
                if (!Directory.Exists(targetFilePath))
                {
                    Directory.CreateDirectory(targetFilePath);
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(Properties.Resources.Status_xml);

                //写入聊天信息至xml   写入发消息人
                XmlNode root = xmlDoc.SelectSingleNode("FileChat/UserStatus");
                root.Attributes["ID"].Value = user.ID.ToString();
                root.Attributes["Status"].Value = "online";
                root.Attributes["DateTime"].Value = DateTime.Now.ToLongTimeString();
                
               

                ////写入时间 
                //XmlElement timeXml = xmlDoc.CreateElement("Time");
                //timeXml.InnerText = DateTime.Now.ToString();
                //root.AppendChild(timeXml);
                ////写入消息内容
                //XmlElement msgXml = xmlDoc.CreateElement("Content");

                //XmlCDataSection msg = xmlDoc.CreateCDataSection("Content");
                //msg.Value = txtSend.Rtf;
                //richTextBox1.SelectionStart = richTextBox1.Text.Length;
                //richTextBox1.SelectionColor = Color.Green;
                //richTextBox1.SelectedText = string.Format("\n{0}  {1}\n", CurrentUser.DisplayName, DateTime.Now.ToString());
                //richTextBox1.SelectionStart = richTextBox1.Text.Length;
                //richTextBox1.SelectedRtf = txtSend.Rtf;
                //richTextBox1.SelectionStart = richTextBox1.Text.Length;
                //richTextBox1.Focus();
                //txtSend.Text = "";
                //msgXml.AppendChild(msg);
                //root.AppendChild(msgXml);
                xmlDoc.Save(msgPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            
        }

        private User GetUser(XmlNode root, string username,string password)
        {
            foreach (XmlElement usernode in root.ChildNodes)
            {
                if (usernode.Attributes["Username"].Value == username && usernode.Attributes["Password"].Value == YiiUtil.MD5(password))
                {
                    User u = new User();
             
                    foreach (PropertyInfo pi in u.GetType().GetProperties())
                    {

                        pi.SetValue(u, usernode.Attributes[pi.Name].Value, null);

                    }
                    return u;
                }
            }
            return null;
        }

    
    }
}
